package com.dycx.cms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.cms.mapper.ArticleTypeMapper;
import com.dycx.cms.dto.ArticleTypeDto;
import com.dycx.cms.entity.ArticleTypeDO;
import com.dycx.cms.service.ArticleTypeService;
import com.dycx.cms.vo.ArticleTypeVO;
import com.dycx.system.service.OssFileService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
@AllArgsConstructor
public class ArticleTypeImpl extends ServiceImpl<ArticleTypeMapper, ArticleTypeDO> implements ArticleTypeService {

    private final OssFileService ossFileService;

    @Override
    public Boolean delete(int typeId) {
        ArticleTypeDO oldEntity = this.getById(typeId);

        if (!this.removeById(typeId)) return false;

        ossFileService.setDeleted(oldEntity.getImage1());
        ossFileService.setDeleted(oldEntity.getImage2());
        ossFileService.setDeleted(oldEntity.getImage3());
        return true;
    }

    /**
     * 添加
     *
     * @param articleTypeDto
     * @return
     */
    @Override
    public Boolean add(ArticleTypeDto articleTypeDto) {
        ArticleTypeDO articleTypeEntity = BeanUtil.toBean(articleTypeDto, ArticleTypeDO.class);

        if (!this.save(articleTypeEntity)) return false;

        ossFileService.setUse(articleTypeDto.getImage1());
        ossFileService.setUse(articleTypeDto.getImage2());
        ossFileService.setUse(articleTypeDto.getImage3());

        articleTypeDto.setId(articleTypeEntity.getId());
        return true;
    }

    /**
     * 修改
     *
     * @param articleTypeDto
     * @return
     */
    @Override
    public Boolean edit(ArticleTypeDto articleTypeDto) {
        ArticleTypeDO oldEntity = this.getById(articleTypeDto.getId());

        ArticleTypeDO articleTypeEntity = BeanUtil.toBean(articleTypeDto, ArticleTypeDO.class);
        if (!this.updateById(articleTypeEntity)) return false;

        ossFileService.setDeleted(oldEntity.getImage1());
        ossFileService.setDeleted(oldEntity.getImage2());
        ossFileService.setDeleted(oldEntity.getImage3());
        ossFileService.setUse(articleTypeDto.getImage1());
        ossFileService.setUse(articleTypeDto.getImage2());
        ossFileService.setUse(articleTypeDto.getImage3());

        return true;
    }

    /**
     * 获取父类下的所有分类
     *
     * @param parentId
     * @return
     */
    @Override
    public List<ArticleTypeVO> getAllList(Integer parentId) {
        return getAllList(parentId, null);
    }

    @Override
    public List<ArticleTypeVO> getAllList(Integer parentId, Boolean status) {
        LambdaQueryWrapper<ArticleTypeDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ArticleTypeDO::getParentId, parentId);
        queryWrapper.eq(ObjectUtil.isNotNull(status), ArticleTypeDO::getStatus, status);
        queryWrapper.orderByDesc(ArticleTypeDO::getSort);

        List<ArticleTypeDO> typeList = this.list(queryWrapper);

        List<ArticleTypeVO> articleTypeVOList = typeList.stream().map(item ->
                BeanUtil.toBean(item, ArticleTypeVO.class)
        ).collect(Collectors.toList());

        for (ArticleTypeVO articleTypeVo : articleTypeVOList) {
            List<ArticleTypeVO> childrenList = getAllList(articleTypeVo.getId(), status);
            if (ObjectUtil.isNotEmpty(childrenList)) {
                articleTypeVo.setChildren(childrenList);
            }
        }

        return articleTypeVOList;
    }

    /**
     * 更新排序
     *
     * @param articleTypeDto
     * @return
     */
    @Override
    public Boolean updateSort(ArticleTypeDto articleTypeDto) {
        ArticleTypeDO articleTypeEntity = new ArticleTypeDO();
        articleTypeEntity.setId(articleTypeDto.getId());
        articleTypeEntity.setSort(articleTypeDto.getSort());
        return this.updateById(articleTypeEntity);
    }

    /**
     * 更新状态
     *
     * @param articleTypeDto
     * @return
     */
    @Override
    public Boolean updateStatus(ArticleTypeDto articleTypeDto) {
        ArticleTypeDO articleTypeEntity = new ArticleTypeDO();
        articleTypeEntity.setId(articleTypeDto.getId());
        articleTypeEntity.setStatus(articleTypeDto.getStatus());
        return this.updateById(articleTypeEntity);
    }

    /**
     * 获取分类id父路径
     *
     * @param id
     * @return
     */
    @Override
    public List<Integer> getArticleTypeIdLevel(int id) {
        List<Integer> idList = new ArrayList<>();

        ArticleTypeDO articleTypeEntity = this.getById(id);

        if (articleTypeEntity.getParentId() != 0) {
            idList.addAll(this.getArticleTypeIdLevel(articleTypeEntity.getParentId()));
        }

        idList.add(id);
        return idList;
    }

    /**
     * 获取分类父路径
     */
    @Override
    public List<ArticleTypeVO> getArticleTypeLevel(List<Integer> dataList, int i) {
        List<ArticleTypeVO> list = new ArrayList<>();

        ArticleTypeDO articleTypeDO = this.getById(dataList.get(i));
        ArticleTypeVO articleTypeVO = BeanUtil.toBean(articleTypeDO, ArticleTypeVO.class);

        if (i < dataList.size() - 1) {
            i++;
            articleTypeVO.setChildren(getArticleTypeLevel(dataList, i));
        }

        list.add(articleTypeVO);
        return list;
    }

    /**
     * 查询父类下第一级分类
     *
     * @param parentId
     * @return
     */
    @Override
    public List<ArticleTypeVO> getListByParentId(Integer parentId) {
        return getListByParentId(parentId, null);
    }

    @Override
    public List<ArticleTypeVO> getListByParentId(Integer parentId, Boolean status) {
        LambdaQueryWrapper<ArticleTypeDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ArticleTypeDO::getParentId, parentId);
        queryWrapper.eq(ObjectUtil.isNotNull(status), ArticleTypeDO::getStatus, status);
        queryWrapper.orderByDesc(ArticleTypeDO::getSort);

        List<ArticleTypeDO> typeList = this.list(queryWrapper);

        List<ArticleTypeVO> articleTypeVOList = new ArrayList<>();

        typeList.forEach(item -> {
            ArticleTypeVO articleTypeVO = BeanUtil.toBean(item, ArticleTypeVO.class);

            //是否有子类
            LambdaQueryWrapper<ArticleTypeDO> queryCount = new LambdaQueryWrapper<>();
            queryCount.eq(ArticleTypeDO::getParentId, item.getId());
            queryCount.eq(ObjectUtil.isNotNull(status), ArticleTypeDO::getStatus, status);
            articleTypeVO.setHasChildren(count(queryCount) > 0);

            articleTypeVOList.add(articleTypeVO);
        });

        return articleTypeVOList;
    }

    /**
     * 对象列表转ids列表
     *
     * @param list
     * @return
     */
    @Override
    public List<Integer> toIntList(List<ArticleTypeVO> list) {
        List<Integer> ids = new ArrayList<>();
        list.forEach(item -> {
            ids.add(item.getId());
            if (ObjectUtil.isNotEmpty(item.getChildren())) {
                ids.contains(toIntList(item.getChildren()));
            }
        });

        return ids;
    }
}
